Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor StatSvc ip recording, fix #2349 #2351

Draft
wants to merge 16 commits into
base: dev
Choose a base branch
from

Conversation

sandtechnology
Copy link
Collaborator

@sandtechnology sandtechnology commented Nov 17, 2022

I'm the beginner for C language, so feel free to let me know if there have any wrong here : D

Changes:

  • Getting server ip from socket directly.
  • Change windows native socket code, only use ipv4 instead and fix unsafe printf usage.
  • Fix wrong bid used in StatSvc.offline.

@Him188
Copy link
Member

Him188 commented Nov 17, 2022

请添加单元测试,至少测试不需要网络请求的 ipv4

@Karlatemp Karlatemp linked an issue Nov 18, 2022 that may be closed by this pull request
@Him188 Him188 added this to the 2.14.0-RC milestone Nov 20, 2022
@Him188
Copy link
Member

Him188 commented Dec 4, 2022

image

test 失败了

@sandtechnology sandtechnology changed the title Turning toIpV4Long function into MMP, fix #2349 Refactor StatSvc ip recording, fix #2349 Dec 6, 2022
@Karlatemp Karlatemp requested a review from Him188 December 7, 2022 04:02
@@ -30,14 +30,15 @@ internal abstract class AbstractCommonNHTestWithSelector :
overrideComponents[BotOfflineEventMonitor] = BotOfflineEventMonitorImpl()
}

val conn = PlatformConn()
val conn get() = PlatformConn(createAddress())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by lazy?

@@ -131,6 +131,16 @@ internal open class NettyNetworkHandler(
return contextResult.await()
}

override fun io.netty.channel.Channel.getConnectedIP(): Long = this.remoteAddress().let { address ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么要有这个lambda?

0L
}
}
}.invoke()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

创建lambda立即invoke只会增加复杂度

@@ -51,7 +57,16 @@ internal actual abstract class AbstractCommonNHTest actual constructor() :
}
}

actual val conn: PlatformConn = NettyNHTestChannel()
actual val conn: PlatformConn get() = PlatformConn(address = createAddress())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by lazy?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JVM那边的测试也是get() 而且这里是创建连接才会去get这个 get()这里应该没问题

@@ -32,8 +37,22 @@ internal actual abstract class AbstractCommonNHTest actual constructor() :
protected actual fun removeOutgoingPacketEncoder() {
}

actual val conn: PlatformConn = PlatformConn()
actual val conn: PlatformConn get() = PlatformConn(createAddress())

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by lazy?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JVM那边的测试也是get() 而且这里是创建连接才会去get这个 get()这里应该没问题

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

属性get有副作用而且每次都创建新实例是不好的

Comment on lines +172 to +181
uOldSSOIp = if (lastDisconnectedIP in 1..2) {
-lastDisconnectedIP
} else {
lastDisconnectedIP
}
uNewSSOIp = if (lastConnectedIP in 1..2) {
-lastDisconnectedIP
} else {
lastConnectedIP
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这部分应该在提供这个 ip 的地方处理

@@ -24,6 +24,7 @@ import kotlin.jvm.JvmName
*/
internal expect class PlatformSocket : Closeable, HighwayProtocolChannel {
val isOpen: Boolean
val connectedIp: Long
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

给特殊意义值留下注释

Comment on lines +47 to +52
actual val connectedIp: Long
get() = if (isOpen) {
sockets.socket_get_connected_ip(socket).toLong()
} else {
0L
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该在这里就直接处理特殊值 (0, 1, ....) 的情况(即切换回负数), 而不是交给外部

@AdoptOSS
Copy link
Contributor

Change windows native socket code, only use ipv4 instead and fix unsafe printf usage.

Why? This should not be applied without special reasons.

@sandtechnology
Copy link
Collaborator Author

Change windows native socket code, only use ipv4 instead and fix unsafe printf usage.

Why? This should not be applied without special reasons.

Because in my test it returning ipv6 first and cause connect failed lol

@AdoptOSS
Copy link
Contributor

AdoptOSS commented Dec 29, 2022 via email

@sandtechnology
Copy link
Collaborator Author

sandtechnology commented Dec 29, 2022

Change windows native socket code, only use ipv4 instead and fix unsafe printf usage.

Why? This should not be applied without special reasons.

Because in my test it returning ipv6 first and cause connect failed lol

You should recheck your network environment first. Even if there is a bug, it’s not wise to disable Ipv6 rather than fixing the bug.

But I just have ipv6 in my local network and could not use ipv6 to connect, it's the most user environment (in China), and currently tencent just provide ipv4 address and a domain, in this case, original native code will be resulted in could not init connection and retrying (we just have this domain in ip list when first init)

@sandtechnology
Copy link
Collaborator Author

If you want to use ipv6 anyway, linux code need to refactor as well since it just support ipv4, and both need to impl a dns query then use a loop to try every ip

@AdoptOSS
Copy link
Contributor

it's the most user environment (in China)

I don't think so, at least almost all ISPs in maintain China begin to distribute IPv6 addresses.

and currently tencent just provide ipv4 address and a domain

This is a powerful point. I'll check it later.

original native code will be resulted in could not init connection and retrying

No, the origin code creates a dual-stack socket (via ipv6only=0). It should not retry if ipv6 is not avaliable.

both need to impl a dns query then use a loop to try every ip

I don't think so. WSAConnectByNameA should try any possible address and choose a best internally.

@AdoptOSS
Copy link
Contributor

AdoptOSS commented Dec 31, 2022 via email

@AdoptOSS AdoptOSS mentioned this pull request Dec 31, 2022
@Him188 Him188 modified the milestones: 2.14.0-RC, 2.15 Jan 2, 2023
@Him188 Him188 added t:enhancement 类型: 现有功能上的优化 s:core 子系统: mirai-core labels Jan 18, 2023
@Him188 Him188 modified the milestones: 2.15, 2.15.0-RC Jan 18, 2023
@Him188 Him188 removed this from the 2.15.0-RC milestone Feb 19, 2023
@Him188
Copy link
Member

Him188 commented Feb 19, 2023

我觉得可以先完善 v4, 之后再考虑 v6

@Him188 Him188 marked this pull request as draft March 20, 2023 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
s:core 子系统: mirai-core t:enhancement 类型: 现有功能上的优化
Projects
None yet
Development

Successfully merging this pull request may close these issues.

java.lang.NumberFormatException: Invalid number format: 'msfwifi'
4 participants